package com.ys.erp.stage_user_service.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ys.erp.common.util.YsFileUtil;
import com.ys.erp.entity.data.bean.UserContract;
import com.ys.erp.entity.service.IUserContractService;
import com.ys.erp.framework.bean.dto.StageEmpDTO;
import com.ys.erp.stage_user_service.bean.condition.UserContractCondition;
import com.ys.erp.stage_user_service.bean.vo.UserContractVO;
import com.ys.erp.stage_user_service.utils.CosUtil;
import com.ys.erp.stage_user_service.utils.JzqUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.List;

/**
 * @author lwj
 * @date 2025/9/18
 */
@RequiredArgsConstructor
@Service
@RefreshScope
@Log4j
public class UserContractService {

    @Value("${cos.server}")
    private String server;

    private final IUserContractService iUserContractService;

    private final JzqUtil jzqUtil;

    private final CosUtil cosUtil;

    /**
     * 用户合同集
     *
     * @param empDto                    登录员工
     * @param con                       请求体
     * @return                          合同集
     */
    public List<UserContractVO> listUserContract(StageEmpDTO empDto, UserContractCondition.ListContractCon con) {
        QueryWrapper<UserContract> contractQw = new QueryWrapper<>();
        contractQw.lambda().eq(UserContract::getUserId, con.getUserId());
        contractQw.lambda().eq(UserContract::getPetCode, con.getCatCode());
        contractQw.lambda().eq(UserContract::getContractStatus, UserContract.Enums.SUCCESS.name());
        List<UserContract> contractList = iUserContractService.list(contractQw);
        return contractList.stream().map(contract -> {
            UserContractVO vo = new UserContractVO();
            vo.setContractName(contract.getName());
            vo.setContractNo(contract.getNumber());
            vo.setSignTime(contract.getSignTime());
            if (ObjectUtils.isNotEmpty(contract.getContractsUrl())) {
                vo.setContractUrl(contract.getContractsUrl());
            } else {
                String s = jzqUtil.getContractDownloadUrl(contract.getApplyNo());
                String fileName = String.valueOf(System.currentTimeMillis());
                try {
                    ByteArrayOutputStream byteArrayOutputStream = YsFileUtil.downloadFile(s);
                    InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
                    String upload = cosUtil.upload(inputStream, fileName + ".pdf", "contract/file/");
                    contract.setContractsUrl(server  + "/" + upload);
                    iUserContractService.updateById(contract);
                } catch (Exception e) {
                    log.info(e.getMessage());
                }
            }
            return vo;
        }).toList();
    }

}
